package com.hhs.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhs.storage.bean.*;
import com.hhs.storage.dao.BaseRoleDao;
import com.hhs.storage.service.BaseMenuRoleService;
import com.hhs.storage.service.BaseRoleService;
import com.hhs.storage.service.BaseRoleUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author shadow
 * @since 2018-09-29
 */
@Service
@Transactional
public class BaseRoleServiceImpl extends ServiceImpl<BaseRoleDao, BaseRole> implements BaseRoleService {
    @Resource
    BaseMenuRoleService jxcMenuRoleService;
    @Resource
    BaseRoleUserService baseRoleUserService;
    @Override
    public Map<String, Object> dele(String id) {
        //先删除关联菜单数据
        jxcMenuRoleService.remove(new QueryWrapper<BaseMenuRole>().eq("role_id_", id));
        //删除关联用户
        baseRoleUserService.remove(new QueryWrapper<BaseRoleUser>().eq("role_id_", id));
        this.remove(new QueryWrapper<BaseRole>().eq("id_", id));
        return null;
    }

    @Override
    public Map<String, Object> putMenus(String roleId, String menuIds) {
        //删除所有旧数据，添加新数据
        jxcMenuRoleService.remove(new QueryWrapper<BaseMenuRole>().eq("role_id_", roleId));
        String[] idArr =  menuIds.split(",");
        for (String s : idArr) {
            if(s.equals("null")){ continue;}
            BaseMenuRole temp = new BaseMenuRole();
            temp.setRoleId(roleId);
            temp.setMenuId(s);
            jxcMenuRoleService.save(temp);
        }
        return null;
    }

    @Override
    public List getByUserId(String userId) {
        List<BaseRole> list = this.baseMapper.getByUserId(userId);
        return list;
    }
    @Override
    public boolean hasRoles(String[] flags, String userId) {
        List<BaseRole> list = this.baseMapper.getByUserId(userId);
        for (BaseRole baseRole : list) {
            for (int i=0;i<flags.length;i++){
                if(flags[i].equals(baseRole.getFlag())){
                    return true;
                }
            }

        }
        return false;
    }
    @Override
    public boolean hasRole(String flag, String userId) {
        List<BaseRole> list = this.baseMapper.getByUserId(userId);
        for (BaseRole baseRole : list) {
            if(flag.equals(baseRole.getFlag())){
                return true;
            }
        }
        return false;
    }
    @Override
    public boolean hasOnlyRole(String flag, String userId) {
        List<BaseRole> list = this.baseMapper.getByUserId(userId);
        if (list.size()==1&&flag.equals(list.get(0).getFlag())){
            return true;
        }else{
            return false;
        }
    }
    @Override
    public Map<String, Object> addOrEdit(BaseRole baseRole) {
        if(StringUtils.isEmpty(baseRole.getId())){
            baseRole.setCreateTime(new Date());
        }
        List<BaseRole> tempList = this.baseMapper.selectList(new QueryWrapper<BaseRole>().eq("name_", baseRole.getName()));

        if(tempList.size()>0){
            //如果角色名称已经存在
            if(StringUtils.isEmpty(baseRole.getId()) ||
                    !baseRole.getId().equals(tempList.get(0).getId())){
                //执行新增操作或者更新操作但不是同一条，则提示重复名称
                return  ResponseInfo.error("角色名称已经存在，不允许重复创建");
            }
        }
        this.saveOrUpdate(baseRole);
        return ResponseInfo.success(null);
    }
}
